package com.syxp.pda.service.system.impl;

import java.util.List;
import java.util.Set;

import org.springframework.transaction.annotation.Transactional;

import com.syxp.pda.core.dao.BaseDao;
import com.syxp.pda.core.service.impl.BaseServiceImpl;
import com.syxp.pda.core.util.ArraysBuilder;
import com.syxp.pda.dao.system.FunctionDefineDao;
import com.syxp.pda.dao.system.RoleDao;
import com.syxp.pda.entity.system.FunctionDefine;
import com.syxp.pda.entity.system.Role;
import com.syxp.pda.service.system.RoleService;

@Transactional
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {

	private RoleDao roleDao;
	private FunctionDefineDao functionDefineDao;
	
	public RoleServiceImpl(BaseDao<Role> baseDao) {
		super(baseDao);
		this.roleDao = (RoleDao) baseDao;
	}

	@Override
	public void update(Role role) {
		Role r = this.loadService(role.getRoleID());
		r.setRoleCode(role.getRoleCode());
		r.setRoleName(role.getRoleName());
		r.setRemarks(role.getRemarks());
		this.updateService(r);
	}
	
	@Override
	public void setPrivilege(int roleId, List funcList) {
		Role role = this.roleDao.get(roleId);
		Set<FunctionDefine> funcSet = role.getFunctionDefines();
		//Set<Department> departSet = role.getDepartments();
		funcSet.clear();
		//departSet.clear();
		this.roleDao.save(role);
		for(Object o: funcList) {
			funcSet.add(this.functionDefineDao.get((Integer)o));
		}
//		for(Object o: departList) {
//			departSet.add((Department)o);
//		}
		this.roleDao.update(role);

	}

	@Override
	public List[] loadPrivilege(int roleId) {
		List[] privilege = new List[2];
		Role role = this.roleDao.get(roleId);
		Set<FunctionDefine> funcSet = role.getFunctionDefines();
		//Set<Department> departSet = role.getDepartments();
		privilege[0] = ArraysBuilder.buildList(funcSet);
		//privilege[1] = ArraysBuilder.buildList(departSet);
		return privilege;
	}

	/**
	 * @return the functionDefineDao
	 */
	public FunctionDefineDao getFunctionDefineDao() {
		return functionDefineDao;
	}

	/**
	 * @param functionDefineDao the functionDefineDao to set
	 */
	public void setFunctionDefineDao(FunctionDefineDao functionDefineDao) {
		this.functionDefineDao = functionDefineDao;
	}

}
